<template>
  <j-modal
    title="新增情报事件"
    :width="1100"
    :visible="visible"
    switchFullscreen
    :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }"
    @cancel="handleCancel"
    :footer="null"
  >
    <a-spin :spinning="confirmLoading">
      <div class="con-content">
        <div class="con-content-top">
          <a-steps :current="current" size="small">
            <a-step v-for="item in steps" :key="item.title" :title="item.title" />
          </a-steps>
        </div>
        <div class="content-form">
          {{ moduleType }}
          <BaseInformation2 v-show="current === 0" ref="baseInformation2" />
          <!-- 案事件和情报配置 -->
          <TargetList1 v-show="current === 1" ref="targetList1" />
          <!-- 模型选择弹窗 -->
          <ModelConfig v-show="current === 2" ref="modelConfig" />
          <div class="footerBox">
            <a-button class="btn" @click="handleCancel">
              取消
            </a-button>
            <a-button v-if="current > 0" style="margin-left: 8px" @click="prev">
              上一步
            </a-button>
            <a-button v-if="current < steps.length - 1" style="margin-left: 8px" type="primary" @click="next">
              下一步
            </a-button>
            <a-button
              type="primary"
              style="margin-left:8px;"
              v-if="current === 2"
              :loading="submitLoading"
              @click="submitHandler"
            >
              发起审批
            </a-button>
          </div>
        </div>
      </div>
    </a-spin>
  </j-modal>
</template>

<script>
import * as caseApi from '@/api/wxSmuggle/caseEvent.js'
import BaseInformation2 from './AddCaseComponents/BaseInformation2'
import TargetList1 from './AddCaseComponents/TargetList1'
import ModelConfig from './AddCaseComponents/ModelConfig'
export default {
  name: 'AddIntellModal',
  components: { BaseInformation2, TargetList1, ModelConfig },
  props: ['targetData', 'moduleType', 'pageType'],
  data() {
    return {
      width: 1100,
      visible: false,
      disableSubmit: false,
      confirmLoading: false,
      current: 0,
      steps: [
        {
          title: '基本信息'
        },
        {
          title: '添加对象'
        },
        {
          title: '选择模型'
        }
      ],
      submitLoading: false,
      formData: {},
      category: 2 //情报事件
    }
  },
  async created() {},
  methods: {
    show(taskId) {
      this.visible = true
      this.current = 0
      this.taskId = taskId
      // console.log('this.taskId')
      // console.log(this.taskId)
      //编辑，读详情
      if (this.taskId != undefined) {
        this.getDetail()
      }
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
    getDetail() {
      caseApi
        .editShow({ taskId: this.taskId })
        .then(res => {
          if (res.code == 200) {
            this.formData = res.result
            this.$refs.baseInformation2.setData1(this.formData.incidentSaveDTO)
            this.$refs.baseInformation2.setData2(this.formData.incidentTaskSaveDTO)
          }
        })
        .catch(() => {
          this.$message.error('回显无数据！')
        })
    },
    next() {
      // 第一页
      if (this.current === 0) {
        let flag = this.$refs.baseInformation2.onValidate()
        if (!flag) {
          return
        }
        //编辑信息回显
        if (this.taskId) {
          console.log(this.formData.incidentTaskUserList)
          this.$refs.targetList1.setData(this.formData.incidentTaskUserList)
        }
      }
      if (this.current === 1) {
        let flag = this.$refs.targetList1.onValidate()
        if (!flag) {
          return
        }
        //编辑信息回显
        if (this.taskId) {
          this.$refs.modelConfig.setData(this.formData.clearIntelligenceConfigDTO)
        }
      }
      this.current++
      // console.log('111111111111')
      // console.log(this.current)
    },
    prev() {
      // if (this.current === 3) {
      //   this.setPage7Value();
      // }
      this.current--
    },
    submitHandler() {
      let ste1Data = {}
      let ste2Data = {}
      let ste3Data = {}
      let incidentData = this.$refs.baseInformation2.getData1()
      let formData1 = Object.assign(incidentData, { category: this.category, pageType: this.pageType })
      let formData2 = this.$refs.baseInformation2.getData2()
      ste1Data = { incidentSaveDTO: formData1, incidentTaskSaveDTO: formData2 }
      ste2Data = { incidentTaskUserList: this.$refs.targetList1.getData() }
      let configData = this.$refs.modelConfig.getData()
      if (configData == undefined) {
        return
      }
      ste3Data = { clearIntelligenceConfigDTO: configData }
      // console.log('ste3Data11')
      // console.log(ste3Data)
      let modelIdObj = { incidentModelIdList: this.$refs.modelConfig.getModelIdList() }
      // 新建和编辑案事件,category案事件1，情报2
      let params = Object.assign(ste1Data, ste2Data, ste3Data, modelIdObj, { taskId: this.taskId })
      console.log('params')
      console.log(params)
      this.submitLoading = true
      caseApi
        .incidentSave(params)
        .then(res => {
          if (res.code == 200) {
            this.submitLoading = false
            this.$message.success('提交成功！')
            this.visible = false
            this.$emit('createSuccess')
          } else {
            this.submitLoading = false
            this.$message.warning(res.message)
          }
        })
        .catch(() => {
          this.submitLoading = false
          this.$message.error(res.message)
        })
    }
  }
}
</script>
<style lang="less" scoped>
.con-content {
  .con-content-top {
    width: 80%;
    margin: 10px auto 50px auto;
  }
  // padding-bottom: 50px;
}
.footerBox {
  display: flex;
  justify-content: flex-end;
  width: 100%;
  padding-top: 20px;
  border-top: 1px solid #ccc;
}
</style>
